﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public partial class hydevmanager_admin_new_album : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        load_photo();
    }

    protected void btnNewAlbum_click(object sne, EventArgs e)
    {
        string folder_name = generate_folder_name(txtAlbumName.Text) + "_" + DateTime.Now.Hour + "h-" + DateTime.Now.Minute + "m-" + DateTime.Now.Second + "s";
        Session.Add("album_folder", folder_name);

        Directory.CreateDirectory(Server.MapPath("~/files_upload/album/" + folder_name));

        if (fupic.HasFile)
        {
            string albumpic_name = fupic.FileName;
            Session.Add("albumpic", albumpic_name);
            fupic.SaveAs(Server.MapPath("~/files_upload/album/" + folder_name + "/" + albumpic_name));
        }
        int len = txtAlbumName.Text.Length;
        DataAccess.ExeNonStored("sp_dev_album_title_Insert", "@title", txtAlbumName.Text, "@picture", Session["albumpic"].ToString(), "@folder_name", folder_name);
        System.Threading.Thread.Sleep(3000);

        int idalbum = DataAccess.ExeSQLGetInt("SELECT MAX(idalbum) FROM dev_album_title");
        Session.Add("idalbum", idalbum);

        Response.Redirect("admin_manager_photoalbum.aspx?idalbum=" + idalbum + "&album_folder=" + folder_name + "");
    }

    public static string generate_folder_name(object Title)
    {
        string strTitle = Title.ToString();
        strTitle = ITNLip.RemoveSign4VietnameseString(strTitle);
        #region RemoveSign4VietnameseString
        //Trim Start and End Spaces.
        strTitle = strTitle.Trim();

        //Trim "-" Hyphen
        strTitle = strTitle.Trim('-');

        strTitle = strTitle.ToLower();
        char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
        strTitle = strTitle.Replace("c#", "C-Sharp");
        strTitle = strTitle.Replace("vb.net", "VB-Net");
        strTitle = strTitle.Replace("asp.net", "Asp-Net");

        //Replace . with - hyphen
        strTitle = strTitle.Replace(".", "-");

        //Replace Special-Characters
        for (int i = 0; i < chars.Length; i++)
        {
            string strChar = chars.GetValue(i).ToString();
            if (strTitle.Contains(strChar))
            {
                strTitle = strTitle.Replace(strChar, string.Empty);
            }
        }

        //Replace all spaces with one "-" hyphen
        strTitle = strTitle.Replace(" ", "-");

        //Replace multiple "-" hyphen with single "-" hyphen.
        strTitle = strTitle.Replace("--", "-");
        strTitle = strTitle.Replace("---", "-");
        strTitle = strTitle.Replace("----", "-");
        strTitle = strTitle.Replace("-----", "-");
        strTitle = strTitle.Replace("----", "-");
        strTitle = strTitle.Replace("---", "-");
        strTitle = strTitle.Replace("--", "-");

        //Run the code again...
        //Trim Start and End Spaces.
        strTitle = strTitle.Trim();

        //Trim "-" Hyphen
        strTitle = strTitle.Trim('-');
        #endregion

        return strTitle;
    }
    protected void grAllphoto_click(object sen, CommandEventArgs e)
    {
        string Argvalue = e.CommandArgument.ToString();
        char[] ch = new char[] { '|' };

        string[] s = ITNLip.SplitString(Argvalue, ch);
        string id = s[0];
        string action = s[1];
        string photo_name = s[2];

        switch (action)
        {
            case "edit":
                txtAlbumName.Focus();
                txtAlbumName.Text = "";
                DataTable dtedit = new DataTable();
                dtedit = DataAccess.ExeSQLToDataTable("select * from dev_album_title where idalbum='" + id + "'");
                if (dtedit.Rows.Count > 0)
                {
                    txtAlbumName.Text = dtedit.Rows[0][1].ToString();
                    Session.Add("foldername", dtedit.Rows[0][6].ToString());
                    Session.Add("idptoto1", id);

                    
                } break;

            case "delete":
                int idphoto = Convert.ToInt32(s[0]);
                DataAccess.ExeNonSQL("DELETE FROM dev_album_detail WHERE idalbum=" + idphoto + "");
                DataAccess.ExeNonSQL("DELETE FROM dev_album_title WHERE idalbum=" + idphoto + "");
                
                delete_photoSelected(photo_name);

                break;
        }

        load_photo();
    }

    protected void load_photo()
    {

        grAllphoto.DataSource = DataAccess.ExeSQLToDataTable("Select * FROM dev_album_title");
        grAllphoto.DataBind();
        //string str=Request.UserHostAddress.ToString();

    }
    protected void save_click(object sen, EventArgs e)
    {
        int idphoto = Convert.ToInt32(Session["idptoto1"].ToString());
        string folder_name = Session["foldername"].ToString();
        if (fupic.HasFile)
        {
            string albumpic_name = fupic.FileName;
            Session.Add("albumpic", albumpic_name);
            fupic.SaveAs(Server.MapPath("~/files_upload/album/" + folder_name + "/" + albumpic_name));
            DataAccess.ExeNonSQL("UPDATE dev_album_title SET title=N'" + txtAlbumName.Text + "',picture=N'" + albumpic_name + "' WHERE idalbum='" + idphoto + "'");
            load_photo();
        }
        else
        {
            DataAccess.ExeNonSQL("UPDATE dev_album_title SET title=N'" + txtAlbumName.Text + "' WHERE idalbum='" + idphoto + "'");
            load_photo();
        }

    }


    protected void delete_photoSelected(string folder_name)
    {
        try
        {
            File.Delete(@Server.MapPath("~/files_upload/album/" + folder_name));
        }
        catch { }
    }
}